Using System;
Using System.Xml;

namespace RTPMethods
{
	public struct ParsedData
	{
		public String type;
		public int value;
	
		public ParsedData(String s, int val)
		{
			type = String.Copy(s);
			value = val;
		}
	}
	class XMLParserClass
	{
		/*
		 *\brief This method receives a byte[] of an new mote packet, and returns an object array of parsed data
		 *
		 *This method receives a byte[] of a mote packet.
		 *The passed document is expected to be in converted format.
		 *If the document is invalid, the method returns a null value.
		 *@param motepacket byte array of the motepacket in XML format
		 *@return ParsedData array containing parsed data, or null if invalid document
		 */
		 public static ParsedData[] MotePacketParser(byte[] xmlpacket)
		{
			//return null if a null or zero length byte array (no data)
            if (xmlpacket == null || xmlpacket.Length == 0)
                return null;

			try
			{
                MemoryStream motepacket = new MemoryStream(xmlpacket);
                XmlTextReader xmlreader = new XmlTextReader(motepacket);
				//find number of elements
				int i = 0;
				while(!xmlreader.EOF)
				{
					if(xmlreader.ReadToFollowing("ParsedDataElement")) 
						i++;
					else //no more parsed data elements found
						break;
				}
				xmlreader.Close();
				motepacket.Close();
				
				if( i == 0)	//no elements found or bad packet
					return null;
					
				//create array to store data
				ParsedData[] tempdata = new ParsedData[i];
				
				//reset XML reader
				motepacket = new MemoryStream(xmlpacket);
				xmlreader = new XmlTextReader(motepacket);
				
				int j, tempvalue;
				String temptype;
				
				for(j = 0; j < i; j++)
				{
					temptype = null;
					tempvalue = 0;
                    if (xmlreader.ReadToFollowing("Name"))
                    {
                        if (xmlreader.Read())
                        {
                            temptype = xmlreader.ReadContentAsString();
                            if (xmlreader.ReadToFollowing("ConvertedValue"))
                            {
                                if (xmlreader.Read())
                                    tempvalue = xmlreader.ReadContentAsInt();
                                else //no data inside "Converted Value" element
                                    temptype = null;
                            }
                            else	//no "Converted Value" element found
                                temptype = null;
                        }
                        else //no data inside "Name" element
                            temptype = null;
                    }
                    else
                        temptype = null;

                    if (temptype == null) //packet is bad somehow
                        return null;                       
                    else
                        tempdata[j] = new ParsedData(temptype, tempvalue);
				}
                return tempdata;
			}
			catch (Exception ex)
			{
				//print an error to error console
				return null;
			}
		}			
	}
}
	
		

			